/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ventanaFacade;

import dao.AbstractFacade;
import modelo.InterfazFacadeModelo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author carlos
 */
public class ModeloTabla extends DefaultTableModel
{
    private AbstractFacade facade;
        
    public ModeloTabla(AbstractFacade facade) 
    {
        super(0,0);
        this.facade=facade;
        String[] encabezado=facade.getEstructuraTabla();
        super.setColumnIdentifiers(encabezado);
        cargarDatos();
       
    }
    
    private void limpiar()
    {
        while(this.getRowCount()>0)
        {
            this.removeRow(0);
        }
    }
    
    public void cargarDatos()
    {
       // this.removeRow(row);
        limpiar();
        Vector<Object> lista=facade.getAll();
        for (Iterator<Object> it = lista.iterator(); it.hasNext();) {
            InterfazFacadeModelo object = (InterfazFacadeModelo) it.next();
            String datos[]=object.getDatos();
            this.addRow(datos);
            
        }
        
                      
    }
    
    public Object getObj(int index)
    {
        return facade.getFind(this.getValueAt(index,0).toString());
    }

    void filtroDatos(String texto) {
        
        limpiar();
        Vector<Object> lista=facade.getAll();
        for (Iterator<Object> it = lista.iterator(); it.hasNext();) {
            InterfazFacadeModelo object = (InterfazFacadeModelo) it.next();
            String datos[]=object.getDatos();
            boolean opcion = false;
         
            for (int j = 0; j < datos.length; j++) {
                String dato=datos[j];
                if (dato.indexOf(texto) != -1) {
                    opcion = true;
                }
            }

            if (opcion) {
                super.addRow(datos);
            }
                   
        }
        
        
        
    }
    
}
